/**
 * 
 */
package org.tis.tools.abf.module.om.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.tis.tools.abf.module.common.entity.Tree;
import org.tis.tools.abf.module.common.entity.enums.YON;
import org.tis.tools.abf.module.common.entity.vo.TreeDetail;
import org.tis.tools.abf.module.om.controller.request.OmOrgUpdateRequest;
import org.tis.tools.abf.module.om.dao.OmOrgMapper;
import org.tis.tools.abf.module.om.entity.OmEmpOrg;
import org.tis.tools.abf.module.om.entity.OmOrg;
import org.tis.tools.abf.module.om.entity.OmPosition;
import org.tis.tools.abf.module.om.entity.enums.OmOrgStatus;
import org.tis.tools.abf.module.om.entity.enums.OmOrgType;
import org.tis.tools.abf.module.om.entity.vo.OmOrgDetail;
import org.tis.tools.abf.module.om.exception.OMExceptionCodes;
import org.tis.tools.abf.module.om.exception.OrgManagementException;
import org.tis.tools.abf.module.om.service.*;
import org.tis.tools.core.utils.StringUtil;
import org.tis.tools.model.exception.i18.ExceptionCodes;

import java.math.BigDecimal;
import java.util.*;

import static org.tis.tools.core.utils.BasicUtil.wrap;


/**
 * <pre>
 * 机构（Organization）管理服务功能的实现类
 * 
 * <pre>
 * 
 * @author megapro
 *
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OmOrgServiceImpl extends ServiceImpl<OmOrgMapper, OmOrg> implements IOmOrgService {

	/** 拷贝新增时，代码前缀  */
	private static final String CODE_HEAD_COPYFROM = "Copyfrom-";

	@Autowired
	private IOrgCodeGenerator orgCodeGenerator;
	@Autowired
	private IOmEmpOrgService omEmpOrgService;
	@Autowired
	private IOmPositionService omPositionService;
	@Autowired
	private IOmEmployeeService omEmployeeService;

	/**
	 * 重新排序： 自增
	 */
	private static final String REORDER_AUTO_PLUS = "plus";
	/**
	 * 重新排序： 自减
	 */
	private static final String REORDER_AUTO_MINUS = "minus";


	/**
	 * 生成机构代码
	 */
	@Override
	public String genOrgCode(String areaCode, String orgDegree) throws OrgManagementException {
		return orgCodeGenerator.genOrgCode(orgDegree, areaCode);
	}

	@Override
	public OmOrg createRootOrg(String areaCode, String orgDegree, String orgName, OmOrgType orgType,String orgAddr,String linkMan,String linkTel,Date startDate,Date endDate,String remark)
			throws OrgManagementException {

		OmOrg org = new OmOrg();
		// 补充机构状态，新增机构初始状态为 停用
		org.setOrgStatus(OmOrgStatus.STOP);
		// 补充父机构，根节点没有父机构,设置为默认值0
		org.setGuidParents("");
		// 新增节点都先算叶子节点 Y
		org.setIsleaf(YON.YES);
		// 设置机构序列,根机构直接用guid
		org.setOrgSeq(org.getGuid());
		//设置排序字段
		EntityWrapper<OmOrg> wrapper = new EntityWrapper<>();
		wrapper.isNull(OmOrg.COLUMN_GUID_PARENTS);
		org.setSortNo(new BigDecimal(selectCount(wrapper)));
		// 收集入参
		org.setOrgCode(genOrgCode(orgDegree.toString(),areaCode.toString()));
		org.setOrgName(orgName);
		org.setOrgType(orgType);
		org.setOrgDegree(orgDegree);
		org.setArea(areaCode);
		org.setOrgAddr(orgAddr);
		org.setLinkMan(linkMan);
		org.setLinkTel(linkTel);
		org.setStartDate(startDate);
		org.setEndDate(endDate);
		org.setRemark(remark);
		insert(org);
		org.setOrgSeq(org.getGuid());
		updateById(org);
		return org;

	}

	@Override
	public OmOrg createChildOrg(String areaCode, String orgDegree, String orgName, OmOrgType orgType, String guidParents,String orgAddr,String linkMan,String linkTel,Date startDate,Date endDate,String remark)
			throws OrgManagementException {
		// 查询父机构信息
		OmOrg parentsOrg = selectById(guidParents);
		if(parentsOrg == null) {
			throw new OrgManagementException(OMExceptionCodes.ORGANIZATION_PARENT_NOT_EXIST, wrap(guidParents));
		}
		String parentsOrgSeq = parentsOrg.getOrgSeq();
		OmOrg org = new OmOrg();
		// 补充信息
		// 补充机构状态，新增机构初始状态为 停用
		org.setOrgStatus(OmOrgStatus.STOP);
		// 补充父机构，根节点没有父机构
		org.setGuidParents(parentsOrg.getGuid());
		// 新增节点都先算叶子节点 Y
		org.setIsleaf(YON.YES);
		String newOrgSeq = parentsOrgSeq + "." + org.getGuid();
		// 设置机构序列,根据父机构的序列+"."+机构的GUID
		org.setOrgSeq(newOrgSeq);
		//设置排序字段
		org.setSortNo(new BigDecimal(selectCount(new EntityWrapper<OmOrg>().eq(OmOrg.COLUMN_GUID_PARENTS,org.getGuidParents()))));
		// 收集入参
		org.setOrgCode(orgCodeGenerator.genOrgCode(orgDegree.toString(), areaCode.toString()));
		org.setOrgName(orgName);
		org.setOrgType(orgType);
		org.setOrgDegree(orgDegree);
		org.setArea(areaCode);
		org.setOrgAddr(orgAddr);
		org.setLinkMan(linkMan);
		org.setLinkTel(linkTel);
		org.setStartDate(startDate);
		org.setEndDate(endDate);
		org.setRemark(remark);
		// 更新父节点机构 是否叶子节点 节点数 最新更新时间 和最新更新人员
		parentsOrg.setIsleaf(YON.NO);
		insert(org);//新增子节点
		updateById(parentsOrg);//更新父节点
		org.setOrgSeq(parentsOrgSeq + "." + org.getGuid());
		updateById(org);
		return org;
	}

	@Override
	public boolean changeOrg(OmOrgUpdateRequest omOrgUpdateRequest) throws OrgManagementException {

		boolean isexist = false;

		Wrapper<OmOrg> wrapper = new EntityWrapper<>();
		wrapper.eq(OmOrg.COLUMN_ORG_CODE,omOrgUpdateRequest.getOrgCode());
		List<OmOrg> lists = selectList(wrapper);

		for (OmOrg omOrg:lists){
			if (!(omOrg.getGuid().equals(omOrgUpdateRequest.getGuid()))){
				return isexist;
			}
		}

		isexist = true;

		OmOrg omOrgQue = selectById(omOrgUpdateRequest.getGuid());

		OmOrg omOrg = new OmOrg();
		//收集参数
		omOrg.setGuid(omOrgUpdateRequest.getGuid());
		omOrg.setOrgCode(omOrgUpdateRequest.getOrgCode());
		omOrg.setOrgName(omOrgUpdateRequest.getOrgName());
		omOrg.setOrgDegree(omOrgUpdateRequest.getOrgDegree());
		omOrg.setOrgType(omOrgUpdateRequest.getOrgType());
		omOrg.setOrgStatus(omOrgUpdateRequest.getOrgStatus());
		omOrg.setGuidParents(omOrgUpdateRequest.getGuidParents());
		if (StringUtil.isEmpty(omOrgUpdateRequest.getGuidParents())){
			omOrg.setOrgSeq(omOrgUpdateRequest.getGuid());
		}else {
			if (omOrgUpdateRequest.getGuidParents().equals(omOrgQue.getGuidParents())){
				omOrg.setOrgSeq(omOrgUpdateRequest.getOrgSeq());
			}else {
				OmOrg omOrgParent = selectById(omOrgUpdateRequest.getGuidParents());
				if (null != omOrgParent){
					omOrg.setOrgSeq(omOrgParent.getOrgSeq()+"."+omOrgUpdateRequest.getGuid());
				}else {
					throw new OrgManagementException(OMExceptionCodes.ORGANIZATION_PARENT_NOT_EXIST);
				}
			}
		}
		omOrg.setOrgAddr(omOrgUpdateRequest.getOrgAddr());
		omOrg.setLinkMan(omOrgUpdateRequest.getLinkMan());
		omOrg.setLinkTel(omOrgUpdateRequest.getLinkTel());
		omOrg.setStartDate(omOrgUpdateRequest.getStartDate());
		omOrg.setEndDate(omOrgUpdateRequest.getEndDate());
		omOrg.setArea(omOrgUpdateRequest.getArea());
		omOrg.setSortNo(omOrgUpdateRequest.getSortNo());
		omOrg.setIsleaf(omOrgUpdateRequest.getIsleaf());
		omOrg.setRemark(omOrgUpdateRequest.getRemark());


		updateById(omOrg);
		return isexist;
	}


	@Override
	public List<TreeDetail> queryOrgTree() throws OrgManagementException {
//		TreeDetail treeDetail = new TreeDetail();
//
//		//创建子机构的list
//		List<Tree> list = new ArrayList<Tree>();
//
//		if ("null".equals(id)){
//			//查询子机构字典
//			Wrapper<OmOrg> wrapper = new EntityWrapper<OmOrg>();
//			wrapper.isNull(OmOrg.COLUMN_GUID_PARENTS);
//			wrapper.orderBy(OmOrg.COLUMN_SORT_NO);
//
//			List<OmOrg> queryList = selectList(wrapper);
//
//			//将子机构set到list中
//			list = setOrgSonToList(queryList);
//		}else {
//			OmOrg omOrg = selectById(id);
//
//			//查询子机构字典
//			Wrapper<OmOrg> wrapper = new EntityWrapper<OmOrg>();
//			wrapper.eq(OmOrg.COLUMN_GUID_PARENTS,id);
//			wrapper.orderBy(OmOrg.COLUMN_SORT_NO);
//
//			List<OmOrg> queryList = selectList(wrapper);
//
//			//将子机构set到list中
//			list = setOrgSonToList(queryList);
//
//			//收集查询出来的结果
//			treeDetail.setGuid(omOrg.getGuid());
//			treeDetail.setLabel(omOrg.getOrgName());
//			treeDetail.setIsleaf(omOrg.getIsleaf());
//		}
//
//		treeDetail.setChildren(list);
//		return treeDetail;

		List<TreeDetail> treeDetails = new LinkedList<>();
		//查询出所有的机构
		Wrapper<OmOrg> orgWrapper = new EntityWrapper<>();
		orgWrapper.orderBy(OmOrg.COLUMN_SORT_NO,true);
		List<OmOrg> allOrgs = selectList(orgWrapper);

		//将数据拼接为TreeDetail结构
		List<TreeDetail> allTrees = setOrgToTreeDetails(allOrgs);
		//将数据拼接为树形结构
		treeDetails = setInAcMenuDetail(allTrees);

		return treeDetails;
	}

	/**
	 * 根据姓名条件查询机构的树结构
	 */
	@Override
	public List<TreeDetail> queryOrgTreeFilter(String name) throws OrgManagementException {
//		TreeDetail treeDetail = new TreeDetail();
//
//		//创建子机构的list
//		List<Tree> list = new ArrayList<>();
//
//		if ("null".equals(parameterRequest.getGuid())){
//			//查询子机构字典
//			Wrapper<OmOrg> wrapper = new EntityWrapper<OmOrg>();
//			wrapper.isNull(OmOrg.COLUMN_GUID_PARENTS);
//			wrapper.orderBy(OmOrg.COLUMN_SORT_NO);
//			if (!StringUtil.isEmpty(parameterRequest.getName())){
//				wrapper.like(OmOrg.COLUMN_ORG_NAME,parameterRequest.getName());
//			}
//
//			List<OmOrg> queryList = selectList(wrapper);
//
//			//将子机构set到list中
//			list = setOrgSonToList(queryList);
//		}else {
//			OmOrg omOrg = selectById(parameterRequest.getGuid());
//
//			//查询子机构字典
//			Wrapper<OmOrg> wrapper = new EntityWrapper<OmOrg>();
//			wrapper.eq(OmOrg.COLUMN_GUID_PARENTS,parameterRequest.getGuid());
//			wrapper.orderBy(OmOrg.COLUMN_SORT_NO);
//			if (!StringUtil.isEmpty(parameterRequest.getName())){
//				wrapper.like(OmOrg.COLUMN_ORG_NAME,parameterRequest.getName());
//			}
//
//			List<OmOrg> queryList = selectList(wrapper);
//
//			//将子机构set到list中
//			list = setOrgSonToList(queryList);
//
//			//收集查询出来的结果
//			treeDetail.setGuid(omOrg.getGuid());
//			treeDetail.setLabel(omOrg.getOrgName());
//			treeDetail.setIsleaf(omOrg.getIsleaf());
//		}
//
//		treeDetail.setChildren(list);
//		return treeDetail;

		List<TreeDetail> treeDetails = new LinkedList<>();
		//查询出所有的机构
		Wrapper<OmOrg> orgWrapper = new EntityWrapper<>();
		orgWrapper.orderBy(OmOrg.COLUMN_SORT_NO,true);
		List<OmOrg> allOrgs = selectList(orgWrapper);

		if (StringUtil.isEmpty(name)){
			//将数据拼接为TreeDetail结构
			List<TreeDetail> allTrees = setOrgToTreeDetails(allOrgs);
			//将数据拼接为树形结构
			treeDetails = setInAcMenuDetail(allTrees);
		}else {
			//查询机构字典
			Wrapper<OmOrg> wrapper = new EntityWrapper<>();
			wrapper.orderBy(OmOrg.COLUMN_SORT_NO,true);
			wrapper.like(OmOrg.COLUMN_ORG_NAME,name);
			List<OmOrg> orgs = selectList(wrapper);

			List<TreeDetail> details = new LinkedList<>();
			if (0 == orgs.size()){
				return treeDetails;
			}else {
				//将数据拼接为TreeDetail结构
				details = setOrgToTreeDetails(orgs);
			}

			//找出所有需要排序的节点信息
			List<TreeDetail> resultList = new LinkedList<>();
			//将数据拼接为TreeDetail结构
			List<TreeDetail> allTrees = setOrgToTreeDetails(allOrgs);
			Map<String,TreeDetail> allMap = new LinkedHashMap<>();
			if (0 != allTrees.size()){
				//查询结果非空,则循环拼接
				for (TreeDetail treeDetail : allTrees){
					if (null != treeDetail){
						allMap.put(treeDetail.getGuid(),treeDetail);
					}
				}
			}

			for (int i = 0; i < details.size(); i++) {
				if (null != details.get(i).getData()) {
					TreeDetail treeDetailSon = details.get(i);
					resultList.add(treeDetailSon);
					TreeDetail treeDetailFather = treeDetailSon;
					while (null != allMap.get(treeDetailSon.getData()).getData()) {
						treeDetailFather = allMap.get(treeDetailSon.getData());
						resultList.add(treeDetailFather);
						treeDetailSon = treeDetailFather;
					}
					resultList.add(allMap.get(treeDetailFather.getData()));
				}
			}

			//将数据拼接为树形结构
			treeDetails = setInAcMenuDetail(resultList);
		}
		return treeDetails;
	}

	//将子机构set到list中
	private List<Tree> setOrgSonToList(List<OmOrg> queryList){

		if (0 == queryList.size()){
			return null;
		}

		List<Tree> list = new ArrayList<Tree>();

		for (OmOrg omOrgQuery: queryList) {
			Tree tree = new Tree();
			tree.setGuid(omOrgQuery.getGuid());
			tree.setLabel(omOrgQuery.getOrgName());
			tree.setIsleaf(omOrgQuery.getIsleaf());
			list.add(tree);
		}
		return list;
	}

	//将工作组数据拼接为(TreeDetail)结构
	private List<TreeDetail> setOrgToTreeDetails(List<OmOrg> queryList){

		List<TreeDetail> list = new LinkedList<>();
		if (0 == queryList.size()){
			return list;
		}

		for (OmOrg omOrgQuery: queryList) {
			if (null != omOrgQuery){
				TreeDetail treeDetail = new TreeDetail();
				treeDetail.setGuid(omOrgQuery.getGuid());
				treeDetail.setCode(omOrgQuery.getOrgCode());
				treeDetail.setLabel(omOrgQuery.getOrgName());
				treeDetail.setIsleaf(omOrgQuery.getIsleaf());
				treeDetail.setData(omOrgQuery.getGuidParents());
				list.add(treeDetail);
			}
		}
		return list;
	}

	private List<TreeDetail> setInAcMenuDetail(List<TreeDetail> resultList){

		List<TreeDetail> detailExits = new LinkedList<>();
		if (0 == resultList.size()){
			return detailExits;
		}

		Map<String,TreeDetail> map = new LinkedHashMap<String, TreeDetail>();
		for (TreeDetail treeDetail : resultList){
			if (null != treeDetail){
				map.put(treeDetail.getGuid(),treeDetail);
			}
		}

		for(String key:map.keySet())
		{
			if (null != map.get(map.get(key).getData())){
				map.get(map.get(key).getData()).getChildrens().add(map.get(key));
			}else {
				detailExits.add(map.get(key));
			}
		}

		return detailExits;
	}

	@Override
	public void delectRoot(String id) throws OrgManagementException {

		//删除父机构下的所有子机构和所有机构和员工的信息
		deleteAllChild(id);
	}

	//删除父机构下的所有子机构
	public void deleteAllChild(String id) throws OrgManagementException{
		//首先删除父机构对应的子机构
		Wrapper<OmOrg> wrapper = new EntityWrapper<OmOrg>();
		wrapper.eq(OmOrg.COLUMN_GUID_PARENTS,id);

		//获取子机构列表
		List<OmOrg> lists = selectList(wrapper);

		if (0 == lists.size()){
			deleteById(id);
			deleteOmEmp(id);
			deletePosition(id);
		}else {
			for (OmOrg omOrg :lists){
				if (null != omOrg){
					deleteAllChild(omOrg.getGuid());
					deleteOmEmp(id);
					deletePosition(id);
				}
			}
			deleteById(id);
			deleteOmEmp(id);
			deletePosition(id);
		}
	}

	//删除机构之后,需要删除机构下的员工信息
	private void deleteOmEmp(String id) throws OrgManagementException{

		//删除机构和员工的中间表信息
		Wrapper<OmEmpOrg> wrapper = new EntityWrapper<OmEmpOrg>();
		wrapper.eq(OmEmpOrg.COLUMN_GUID_ORG,id);

		List<OmEmpOrg> list = omEmpOrgService.selectList(wrapper);
		if ( 0 != list.size() ){
			omEmpOrgService.delete(wrapper);
		}

		//删除机构下的员工
//		Wrapper<OmEmployee> wrapper = new EntityWrapper<>();
//		wrapper.eq(OmEmployee.COLUMN_GUID_ORG,id);
//
//		List<OmEmployee> list = omEmployeeService.selectList(wrapper);
//		if (0 != list.size()){
//			for (OmEmployee omEmployee : list){
//				if (null != omEmployee){
//					omEmployeeService.moveEmp(omEmployee.getGuid());
//				}
//			}
//		}

	}

	//删除机构之后,删除机构下的岗位
	private void deletePosition(String id) throws OrgManagementException{

		Wrapper<OmPosition> wrapper = new EntityWrapper<>();
		wrapper.eq(OmPosition.COLUMN_GUID_MAPPING,id);
		List<OmPosition> list = omPositionService.selectList(wrapper);
		for (OmPosition omPosition:list){
			if (omPosition != null){
				omPositionService.deleteRoot(omPosition.getGuid());
			}
		}
	}

	@Override
	public boolean moveOrg(String orgCode, String fromParentsOrgCode, String toParentsOrgCode, int toSortNo) throws OrgManagementException {
		return false;
	}

	@Override
	public OmOrg copyOrg(String copyFromOrgCode) throws OrgManagementException {
		return null;
	}

	@Override
	public OmOrg copyOrgDeep(String copyFromOrgCode, boolean copyOrgRole, boolean copyPosition, boolean copyPositionRole, boolean copyGroup, boolean copyGroupRole) throws OrgManagementException {
		return null;
	}

	@Override
	public OmOrg enabledOrg(String orgCode, Date startDate, Date endDate) throws OrgManagementException {
		return null;
	}

	@Override
	public OmOrg reenabledOrg(String orgCode) throws OrgManagementException {
		return null;
	}

	@Override
	public OmOrg disabledOrg(String orgCode) throws OrgManagementException {
		return null;
	}

	@Override
	public OmOrg cancelOrg(String orgCode) throws OrgManagementException {
		return null;
	}

	@Override
	public OmOrg deleteEmptyOrg(String orgCode) throws OrgManagementException {
		return null;
	}

	@Override
	public OmOrg queryOrg(String orgCode) {
		return null;
	}

	@Override
	public List<OmOrg> queryOrgsByName(String name) {
		return null;
	}

	@Override
	public List<OmOrg> queryChilds(String orgCode) {
		return null;
	}

	@Override
	public List<OmOrg> queryAllChilds(String orgCode) {
		return null;
	}

	@Override
	public List<OmOrg> queryChildsByCondition(String orgCode, OmOrg orgCondition) {
		return null;
	}

	@Override
	public List<OmOrg> queryAllRoot() {
		return null;
	}

	@Override
	public List<OmOrg> queryAllOrg() {
		Wrapper<OmOrg> wrapper = new EntityWrapper<OmOrg>();
		return selectList(wrapper);
	}

	/**
	 * 级联查询所有机构
	 */
	@Override
	public List<OmOrgDetail> cascadeQueryAll() throws OrgManagementException {

		List<OmOrgDetail> orgDetailList = new ArrayList<OmOrgDetail>();

		//查询所有父机构
		Wrapper<OmOrg> orgParentWrapper = new EntityWrapper<OmOrg>();
		orgParentWrapper.isNull(OmOrg.COLUMN_GUID_PARENTS);
		orgParentWrapper.orderBy(OmOrg.COLUMN_SORT_NO);
		List<OmOrg> orgParentList = selectList(orgParentWrapper);

		if (0 != orgParentList.size()){
			for (OmOrg omOrg : orgParentList){
				if (null != omOrg){
					Wrapper<OmOrg> orgSonWrapper = new EntityWrapper<OmOrg>();
					orgSonWrapper.eq(OmOrg.COLUMN_GUID_PARENTS,omOrg.getGuid());
					orgSonWrapper.orderBy(OmOrg.COLUMN_SORT_NO);
					List<OmOrg> orgSonList = selectList(orgSonWrapper);
					if (0 != orgSonList.size()){
						OmOrgDetail omOrgDetail = new OmOrgDetail(omOrg,orgSonList);
						orgDetailList.add(omOrgDetail);
					}else {
						OmOrgDetail omOrgDetail = new OmOrgDetail(omOrg,null);
						orgDetailList.add(omOrgDetail);
					}
				}
			}
		}

		return orgDetailList;
	}

	/**
	 * 机构移动
	 */
	@Override
	public OmOrg moveOrg(String targetGuid, String moveGuid, BigDecimal order) throws OrgManagementException {

		// 查询移动的机构信息及其下属的所有机构信息
		OmOrg omOrg = selectById(moveGuid);
		if (omOrg == null) {
			throw new OrgManagementException(ExceptionCodes.NOT_FOUND_WHEN_QUERY);
		}

		List<OmOrg> childOrgList = selectList(new EntityWrapper<OmOrg>().like(OmOrg.COLUMN_ORG_SEQ, moveGuid));
		// 目标机构节点
		OmOrg goalOrg= new OmOrg();
		if (!"null".equals(targetGuid)){
			goalOrg= selectById(targetGuid);
			if (goalOrg == null) {
				throw new OrgManagementException(ExceptionCodes.NOT_FOUND_WHEN_QUERY);
			}
		}

		// 源机构节点
		// 源机构GUID
		String sourceGuid = omOrg.getGuidParents();
		// 源机构显示顺序
		BigDecimal sourceSortNo = omOrg.getSortNo();
		String sourceSeq = omOrg.getOrgSeq();

		// 处理移动机构信息
		// 改变显示顺序
		omOrg.setSortNo(order);
		// 重新排序源机构下的子机构自减
		if (StringUtil.isNotEmpty(sourceGuid)){
			this.baseMapper.reorderOrg(sourceGuid, sourceSortNo, REORDER_AUTO_MINUS);
		}else {
			this.baseMapper.reorderOrgNoParentId(sourceSortNo,REORDER_AUTO_MINUS);
		}
		if ("null".equals(targetGuid)){
			// 改变序列
			omOrg.setOrgSeq(omOrg.getGuid());
			// 改变父机构信息
			omOrg.setGuidParents(null);
			this.baseMapper.reorderOrgNoParentId(order, REORDER_AUTO_PLUS);
		}else {
			omOrg.setOrgSeq(goalOrg.getOrgSeq()+ "." + moveGuid);
			omOrg.setGuidParents(targetGuid);
			// 重新排序目标机构下的子机构自增
			this.baseMapper.reorderOrg(targetGuid, order, REORDER_AUTO_PLUS);
		}

		// 更改移动的重组机构信息
		updateAllColumnById(omOrg);
		// 如果改变了父节点需要同步改变子节点
		if (!"null".equals(targetGuid)){
			if (!targetGuid.equals(omOrg.getGuidParents())) {
				// 更改移动机构下的子机构
				for (OmOrg omOrgQue : childOrgList) {
					// 排除当前移动机构
					if (!StringUtil.equals(omOrgQue.getGuid(), moveGuid)) {
						// 更新机构序列
						// update 表名 set 字段名=REPLACE (字段名,'原来的值','要修改的值')
						String seq = omOrgQue.getOrgSeq();
						OmOrg omOrgUpdate = new OmOrg();
						omOrgUpdate.setGuid(omOrgQue.getGuid());
						omOrgUpdate.setOrgSeq(seq.replace(sourceSeq, omOrg.getOrgSeq()));
						updateById(omOrgUpdate);
					}
				}
			}
		}

		return omOrg;
	}
}
